<?php

/**
 * 首页焦点图修改AJAX
 * 
 * @copyright zhanghuan
 * @since 2012-03-23
 */

include_once($_SERVER["DOCUMENT_ROOT"] . "/core/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/core/admin.inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/include/foodv2/index/db_food_focus_pic_index.class.php");

$g_cgival = array();
$g_pro = array();
$g_show = array();


function check_cgi_pro()
{
	global $g_cgival, $g_pro, $g_show;
	
	//check refer  -  add by xinwei
	if(! security_check::check_refer())
	{
		throw new Exception("来路不明");
	}
	
	//pic_id
	cgi::both($g_cgival["pic_id"], "pic_id", "");
	$g_cgival["pic_id"] = intval($g_cgival["pic_id"]);

	
	//描述
	cgi::both($g_cgival["pic_describe"], "pic_describe", "");
	$g_cgival["pic_describe"] = string::un_script_code($g_cgival["pic_describe"]);
	$g_cgival["pic_describe"] = string::un_html($g_cgival["pic_describe"]);

	//链接
	cgi::both($g_cgival["pic_link"], "pic_link", "");
	$g_cgival["pic_link"] = string::un_script_code($g_cgival["pic_link"]);
	$g_cgival["pic_link"] = string::un_html($g_cgival["pic_link"]);
	
	if (!string::is_url($g_cgival["pic_link"]))
	{
		throw new Exception("非法URL地址！");
	}
}

function get_data()
{
	global $g_cgival, $g_pro, $g_show;
	
	$db = new db_food_focus_pic_index();
	$admin_log = new admin_log();
	
	$site_id = admin::admin_get_user_site_id();	// 拿到当前站点的site_id
	$admin_id = admin::admin_get_login_uid();	// 拿到当前管理员ID
	$admin_name = admin::admin_get_login_user_name();	// 拿到当前管理员用户名
	
	$pic_id = $g_cgival["pic_id"];
	$pic_describe = $g_cgival["pic_describe"];
	$pic_link = $g_cgival["pic_link"];
	
	$img_src = "";
	
	
// 	print_r($_FILES);
	
	if ($_FILES['file1'] and $_FILES["file1"]["tmp_name"] != "")
	{
	
		try
		{
			/* 上传图片 */
			$upload = new s3_upload();
			$filename = "/index_focus_pic/".time();

				
			$data = $upload->show($_FILES['file1'])->zomm("480,320")->save($filename);
			$data_small = $upload->show($_FILES['file1'])->zomm("101,75")->save($filename . "_small");

// 			print_r($data);
// 			print_r($data_small);
			
			if (is_array($data))
			{
				$img_src = $data["src"];
			}
			if (is_array($data))
			{
				$img_src_small = $data_small["src"];
			}			
		}
		catch (Exception $e)
		{
			throw new Exception($e->getMessage(), 1);
		}
	}
	
	
	
	$db  = new db_food_focus_pic_index();
// 	$db->debug = true;
	if ($pic_id > 0)
	{
		// 编辑
		
		$ret = $db->admin_update_item($admin_id, $admin_name, $site_id, $pic_id, $pic_describe, $img_src, $img_src_small, $pic_link);
		if ($ret == false)
		{
			throw new Exception("抱歉，更新失败");
		}
		
		$admin_log->admin_insert_log_info($admin_id, $site_id, 15, "编辑 首页焦点图id={$pic_id}");
	}
	else
	{
		// 新增
		
		$ret = $db->admin_insert_data($site_id, $pic_describe, $img_src, $img_src_small, $pic_link, $admin_id, $admin_name);
		if ($ret == false)
		{
			throw new Exception("抱歉，更新失败");
		}
		
		$admin_log->admin_insert_log_info($admin_id, $site_id, 15, "新增 首页焦点图id={$ret}");
	}
	

}

try
{
	admin_check_login();
	
	check_cgi_pro();
	get_data();
	
	$g_show["error"] = 0;
	$g_show["errmsg"] = '';
}
catch(Exception $e)
{
	$g_show["error"] = 1;
	$g_show["errmsg"] = $e->getMessage();
}
show_pro();
exit;

function show_pro()
{
	global $g_cgival, $g_pro, $g_show;
	if(isset($_GET['debug']))
		print_r($g_show);
	echo json_encode($g_show);
}
?>